<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-home"></i>后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>相册列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-button @click="loadAlbumList" style="float: right">刷新</el-button>
    <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
      <el-table-column prop="id" label="ID" align="center" width="180"></el-table-column>
      <el-table-column prop="name" label="相册名称" align="center" width="180"></el-table-column>
      <el-table-column prop="description" label="描述" align="center" width="180"></el-table-column>
      <el-table-column prop="sort" label="排序序号" ></el-table-column>
      <el-table-column label="操作" >
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle
            @click="openEditDialog(scope.row)" ></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
            @click="openHandleDelete(scope.row)" ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--  弹出的修改相册表单  -->
    <el-dialog title="修改相册" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="相册id" :label-width="formLabelWidth" >
          <el-input v-model="form.id" autocomplete="off" :disabled=true></el-input>
        </el-form-item>
        <el-form-item label="相册名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth">
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序序号" :label-width="formLabelWidth">
          <el-input v-model="form.sort" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEdit">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "AlbumListView",
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {
        name:'',
        description:'',
        sort:''
      },
      formLabelWidth: '120px'
    }
  },
  created(){
    console.log("created...")
  },
  mounted() {
    console.log("mounted...")
    this.loadAlbumList()
  },
  methods: {
    tableRowClassName({row, rowIndex}) {
      if (rowIndex === 1) {
        return 'warning-row';
      } else if (rowIndex === 3) {
        return 'success-row';
      }
      return '';
    },
    loadAlbumList(){
      console.log("loadAlbumList...")
      let url = "http://localhost:9080/albums"
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .get(url).then(response =>{
        console.log("response参数：")
        console.log(response)
        let responseBody = response.data;
        console.log("response的data参数：")
        console.log(responseBody)
        this.tableData = responseBody.data
      })
    },
    openHandleDelete(album) {
      this.$confirm('是否删除【'+album.name+'(id='+album.id+')】', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
          this.handleDelete(album)
      }).catch(() => {
      });
    },
    handleDelete(album){
      // alert("你点击了【"+name+"(id="+id+")】的删除按钮")
      let url = 'http://localhost:9080/albums/'+album.id+'/delete'
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .post(url).then(response=>{
        let responseBody = response.data
        if(responseBody.state != 20000){
          this.$message.error("删除失败")
        }
        this.loadAlbumList()
      })
    },
    openEditDialog(album){
      this.dialogFormVisible = true;
      let url = 'http://localhost:9080/albums/'+album.id+'/info';
      console.log(url);
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .get(url).then(response=>{
        console.log("查询成功")
        let responseBody = response.data;
        if(responseBody.state == 20000){
          this.form = responseBody.data;
        }
      })
    },
    handleEdit(){
      let formData = this.qs.stringify(this.form);
      let url = "http://localhost:9080/albums/" + this.form.id + "/update"
      console.log('修改的请求url：' + url)
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .post(url,formData).then(response=>{
            let responseBody = response.data
        if (responseBody.state == 20000){
          this.dialogFormVisible = false
          this.loadAlbumList()
          this.$message.success("修改成功")
        }else if(responseBody.state == 40400){
          this.dialogFormVisible = false
          this.loadAlbumList()
          this.$message.error(responseBody.message)
        }else if(responseBody.state == 40900){
          this.$message.error(responseBody.message)
        }else {
          this.$message.error(responseBody.message)
        }
      })
    }
  }
}
</script>

<style scoped>

</style>